﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SELF
{
    class IgnoreMouseClickMessageFilter : IMessageFilter
    {
        private Control _Parent { get; set; }
        private Control _Target { get; set; }
        //
        //Application.AddMessageFilter(new IgnoreMouseClickMessageFilter(this,axWindowsMediaPlayer1));
        //
        public IgnoreMouseClickMessageFilter(Control parent, Control target)
        {
            _Parent = parent;
            _Target = target;
        }

        public bool PreFilterMessage(ref Message m)
        {
            try
            {
                if (_Parent == null)
                    return false;

                // Get the Control beneath the current X, Y coordinates
                // of the cursor
                Control child = _Parent.GetChildAtPoint(Cursor.Position);
                if (child == null)
                    return false;

                // Determine if the Control over which the cursor is currently
                // hovering if the target control for which we want to ignore any
                // mouse click activity
                if (child != _Target)
                    return false;

                // At this point, we have identified that the cursor is hovering
                // above our target Control; now we need to determine if the current
                // window message is one that should be ignored...
                if (m.Msg == 0x204 /* WM_RBUTTONDOWN */
                        || m.Msg == 0x205 /* WM_RBUTTONUP */
                        || m.Msg == 0x203 /* WM_LBUTTONDBLCLK*/)
                //m.Msg == 0x202 /* WM_LBUTTONUP*/
                {
                    // Return true to prevent the current window message from being
                    // dispatched any further
                    return true;
                }

                // The current window message is one that should not ignore
                return false;
            }
            catch (Exception ex)
            {
                return false;
                //do nothing.
            }
        }
    }
}
